home *** CD-ROM | disk | FTP | other *** search
- //***********************************************************************
- //
- // Paint6View.cpp
- //
- //***********************************************************************
-
- #include <afxwin.h>
- #include "Resource.h"
- #include "CLine.h"
- #include "Paint6Doc.h"
- #include "Paint6View.h"
-
- IMPLEMENT_DYNCREATE (CPaintView, CScrollView)
-
- BEGIN_MESSAGE_MAP (CPaintView, CScrollView)
- ON_WM_LBUTTONDOWN ()
- ON_WM_MOUSEMOVE ()
- ON_WM_LBUTTONUP ()
- ON_WM_CONTEXTMENU ()
- END_MESSAGE_MAP ()
-
- void CPaintView::OnInitialUpdate ()
- {
- SetScrollSizes (MM_TEXT, CSize (2048, 2048));
- CScrollView::OnInitialUpdate ();
- }
-
- void CPaintView::OnUpdate (CView* pSender, LPARAM lHint, CObject* pHint)
- {
- if (pHint != NULL) {
- CClientDC dc (this);
- OnPrepareDC (&dc);
- ((CLine*) pHint)->Draw (&dc);
- return;
- }
- CScrollView::OnUpdate (pSender, lHint, pHint);
- }
-
- void CPaintView::OnDraw (CDC* pDC)
- {
- CPaintDoc* pDoc = GetDocument ();
- int nCount = pDoc->GetLineCount ();
-
- if (nCount) {
- for (int i=0; i<nCount; i++)
- pDoc->GetLine (i)->Draw (pDC);
- }
- }
-
- void CPaintView::OnLButtonDown (UINT nFlags, CPoint point)
- {
- CClientDC dc (this);
- OnPrepareDC (&dc);
- dc.DPtoLP (&point);
-
- m_ptFrom = point;
- m_ptTo = point;
- SetCapture ();
- }
-
- void CPaintView::OnMouseMove (UINT nFlags, CPoint point)
- {
- if (GetCapture () == this) {
- CClientDC dc (this);
- OnPrepareDC (&dc);
- dc.DPtoLP (&point);
-
- InvertLine (&dc, m_ptFrom, m_ptTo);
- InvertLine (&dc, m_ptFrom, point);
- m_ptTo = point;
- }
- }
-
- void CPaintView::OnLButtonUp (UINT nFlags, CPoint point)
- {
- if (GetCapture () == this) {
- ReleaseCapture ();
- CClientDC dc (this);
- OnPrepareDC (&dc);
- dc.DPtoLP (&point);
- InvertLine (&dc, m_ptFrom, m_ptTo);
-
- CLine* pLine = GetDocument ()->AddLine (m_ptFrom, point);
- if (pLine != NULL) {
- pLine->Draw (&dc);
- GetDocument ()->UpdateAllViews (this, 0, pLine);
- }
- }
- }
-
- void CPaintView::InvertLine (CDC* pDC, CPoint ptFrom, CPoint ptTo)
- {
- int nOldMode = pDC->SetROP2 (R2_NOT);
-
- pDC->MoveTo (ptFrom);
- pDC->LineTo (ptTo);
-
- pDC->SetROP2 (nOldMode);
- }
-
- void CPaintView::OnContextMenu (CWnd* pWnd, CPoint point)
- {
- CMenu menu;
- menu.LoadMenu (IDR_CONTEXTMENU);
- CMenu* pContextMenu = menu.GetSubMenu (0);
-
- for (int i=0; i<8; i++)
- pContextMenu->ModifyMenu (ID_COLOR_BLACK + i,
- MF_BYCOMMAND | MF_OWNERDRAW, ID_COLOR_BLACK + i);
-
- pContextMenu->TrackPopupMenu (TPM_LEFTALIGN | TPM_LEFTBUTTON |
- TPM_RIGHTBUTTON, point.x, point.y, AfxGetMainWnd ());
- }
-